package cn.afactory.tygz.controller;

import android.content.Context;
import android.os.Handler;

import java.util.ArrayList;

import cn.afactory.tygz.cache.LocalJsonCache;
import cn.afactory.tygz.model.MessageDetailList;
import cn.afactory.tygz.model.PrivateUserInfo;
import cn.afactory.tygz.model.PrivateUserInfoList;
import cn.afactory.tygz.model.SnsUserInfoList;
import cn.afactory.tygz.model.SysMessageItemList;
import cn.afactory.tygz.net.NetManager;
import cn.afactory.tygz.net.lsn.OnResponseListener;
import cn.afactory.tygz.ui.base.entity.BannerInfo;
import cn.afactory.tygz.ui.base.entity.BannerInfoList;


public class MessageController extends BaseLogicController {
    public static final int MSG_LOAD_SYS_MSG_LIST_OK = 0x1001;
    public static final int MSG_LOAD_SYS_MSG_LIST_KO = 0x1002;

    public static final int MSG_LOAD_PRIVATE_USER_LIST_OK = 0x1003;
    public static final int MSG_LOAD_PRIVATE_USER_LIST_KO = 0x1004;

    public static final int MSG_LOAD_PRIVATE_MESSAGE_DETAIL_OK = 0x1005;
    public static final int MSG_LOAD_PRIVATE_MESSAGE_DETAIL_KO = 0x1006;

    private Context mContext;
    private Handler mHandler;
    public MessageController(Context context, Handler handler) {
        super(context, handler);
        mContext = context;
    }

    /**
     * 获取系统消息列表
     * @param page
     * @param row
     */
    public void doGetSysMsgList(int page,int row,long addTime){
        NetManager.getInstance(mContext).doGetSysMsgList(page,row, addTime,new OnResponseListener<SysMessageItemList>(){

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_LOAD_SYS_MSG_LIST_KO, errorCode, 0, errorMessage);
            }

            @Override
            public void onComplete(boolean isOK, SysMessageItemList result, int errorCode, String errorMsg) {
                if (isOK) {
                    if (result == null) {
                        result = new SysMessageItemList();
                    }
                    sendMessage(MSG_LOAD_SYS_MSG_LIST_OK, result);
                    return;
                }
                sendMessage(MSG_LOAD_SYS_MSG_LIST_KO, errorCode, 0, errorMsg);
            }
        });
    }

    /**
     * 获取私信用户列表
     * @param page
     * @param row
     * @param addTime
     */
    public void doGetPrivateUserList(int page,int row ,long addTime){
        NetManager.getInstance(mContext).doGetPrivateUserList(page, row, addTime, new OnResponseListener<PrivateUserInfoList>() {

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_LOAD_PRIVATE_USER_LIST_KO, errorCode, 0, errorMessage);
            }

            @Override
            public void onComplete(boolean isOK, PrivateUserInfoList result, int errorCode, String errorMsg) {
                if (isOK) {
                    if (result == null) {
                        result = new PrivateUserInfoList();
                    }
                    sendMessage(MSG_LOAD_PRIVATE_USER_LIST_OK, result);
                    return;
                }
                sendMessage(MSG_LOAD_PRIVATE_USER_LIST_KO, errorCode, 0, errorMsg);
            }
        });
    }

    /**
     * 获取私信详情信息的列表
     * @param toUserId
     * @param page
     * @param row
     */
    public void doGetPrivateMsgList(long toUserId,int page,int row,long addTime){
        NetManager.getInstance(mContext).doGetPrivateMsgList(toUserId,page, row, addTime, new OnResponseListener<MessageDetailList>() {

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                sendMessage(MSG_LOAD_PRIVATE_MESSAGE_DETAIL_KO, errorCode, 0, errorMessage);
            }

            @Override
            public void onComplete(boolean isOK, MessageDetailList result, int errorCode, String errorMsg) {
                if (isOK) {
                    if (result == null) {
                        result = new MessageDetailList();
                    }
                    sendMessage(MSG_LOAD_PRIVATE_MESSAGE_DETAIL_OK, result);
                    return;
                }
                sendMessage(MSG_LOAD_PRIVATE_MESSAGE_DETAIL_KO, errorCode, 0, errorMsg);
            }
        });
    }
}
